home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / samba.idb / usr / samba / src / source / ubiqx / ubi_SplayTree.h.z / ubi_SplayTree.h
Encoding:
C/C++ Source or Header  |  1998-10-28  |  15.5 KB  |  336 lines

  1. #ifndef ubi_SplayTree_H
  2. #define ubi_SplayTree_H
  3. /* ========================================================================== **
  4.  *                              ubi_SplayTree.h
  5.  *
  6.  *  Copyright (C) 1993,1995 by Christopher R. Hertel
  7.  *
  8.  *  Email: crh@ubiqx.mn.org
  9.  * -------------------------------------------------------------------------- **
  10.  *
  11.  *  This module implements "splay" trees.  Splay trees are binary trees
  12.  *  that are rearranged (splayed) whenever a node is accessed.  The
  13.  *  splaying process *tends* to make the tree bushier (improves balance),
  14.  *  and the nodes that are accessed most frequently *tend* to be closer to
  15.  *  the top.
  16.  *
  17.  *  References: "Self-Adjusting Binary Search Trees", by Daniel Sleator and
  18.  *              Robert Tarjan.  Journal of the Association for Computing
  19.  *              Machinery Vol 32, No. 3, July 1985 pp. 652-686
  20.  *
  21.  * -------------------------------------------------------------------------- **
  22.  *
  23.  *  This library is free software; you can redistribute it and/or
  24.  *  modify it under the terms of the GNU Library General Public
  25.  *  License as published by the Free Software Foundation; either
  26.  *  version 2 of the License, or (at your option) any later version.
  27.  *
  28.  *  This library is distributed in the hope that it will be useful,
  29.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  30.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  31.  *  Library General Public License for more details.
  32.  *
  33.  *  You should have received a copy of the GNU Library General Public
  34.  *  License along with this library; if not, write to the Free
  35.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  36.  *
  37.  * -------------------------------------------------------------------------- **
  38.  *
  39.  * Log: ubi_SplayTree.h,v
  40.  * Revision 2.6  1997/12/23 04:02:20  crh
  41.  * In this version, all constants & macros defined in the header file have
  42.  * the ubi_tr prefix.  Also cleaned up anything that gcc complained about
  43.  * when run with '-pedantic -fsyntax-only -Wall'.
  44.  *
  45.  * Revision 2.5  1997/07/26 04:15:46  crh
  46.  * + Cleaned up a few minor syntax annoyances that gcc discovered for me.
  47.  * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
  48.  *
  49.  * Revision 2.4  1997/06/03 05:22:56  crh
  50.  * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
  51.  * problems.
  52.  *
  53.  * Revision 2.3  1995/10/03 22:19:37  CRH
  54.  * Ubisized!
  55.  * Also, added the function ubi_sptSplay().
  56.  *
  57.  * Revision 2.1  95/03/09  23:55:04  CRH
  58.  * Added the ModuleID static string and function.  These modules are now
  59.  * self-identifying.
  60.  * 
  61.  * Revision 2.0  95/02/27  22:34:55  CRH
  62.  * This module was updated to match the interface changes made to the
  63.  * ubi_BinTree module.  In particular, the interface to the Locate() function
  64.  * has changed.  See ubi_BinTree for more information on changes and new
  65.  * functions.
  66.  *
  67.  * The revision number was also upped to match ubi_BinTree.
  68.  *
  69.  *
  70.  * Revision 1.0  93/10/15  22:59:36  CRH
  71.  * With this revision, I have added a set of #define's that provide a single,
  72.  * standard API to all existing tree modules.  Until now, each of the three
  73.  * existing modules had a different function and typedef prefix, as follows:
  74.  *
  75.  *       Module        Prefix
  76.  *     ubi_BinTree     ubi_bt
  77.  *     ubi_AVLtree     ubi_avl
  78.  *     ubi_SplayTree   ubi_spt
  79.  *
  80.  * To further complicate matters, only those portions of the base module
  81.  * (ubi_BinTree) that were superceeded in the new module had the new names.
  82.  * For example, if you were using ubi_AVLtree, the AVL node structure was
  83.  * named "ubi_avlNode", but the root structure was still "ubi_btRoot".  Using
  84.  * SplayTree, the locate function was called "ubi_sptLocate", but the next
  85.  * and previous functions remained "ubi_btNext" and "ubi_btPrev".
  86.  *
  87.  * This was not too terrible if you were familiar with the modules and knew
  88.  * exactly which tree model you wanted to use.  If you wanted to be able to
  89.  * change modules (for speed comparisons, etc), things could get messy very
  90.  * quickly.
  91.  *
  92.  * So, I have added a set of defined names that get redefined in any of the
  93.  * descendant modules.  To use this standardized interface in your code,
  94.  * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
  95.  * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or
  96.  * datatype names for the module that you are using.  Just remember to
  97.  * include the header for that module in your program file.  Because these
  98.  * names are handled by the preprocessor, there is no added run-time
  99.  * overhead.
  100.  *
  101.  * Note that the original names do still exist, and can be used if you wish
  102.  * to write code directly to a specific module.  This should probably only be
  103.  * done if you are planning to implement a new descendant type, such as
  104.  * red/black trees.  CRH
  105.  *
  106.  * Revision 0.0  93/04/21  23:07:13  CRH
  107.  * Initial version, written by Christopher R. Hertel.
  108.  * This module implements Splay Trees using the ubi_BinTree module as a basis.
  109.  *
  110.  * ========================================================================== **
  111.  */
  112.  
  113. #include "ubi_BinTree.h" /* Base binary tree functions, types, etc.  */
  114.  
  115. /* ========================================================================== **
  116.  * Function prototypes...
  117.  */
  118.  
  119. ubi_trBool ubi_sptInsert( ubi_btRootPtr  RootPtr,
  120.                           ubi_btNodePtr  NewNode,
  121.                           ubi_btItemPtr  ItemPtr,
  122.                           ubi_btNodePtr *OldNode );
  123.   /* ------------------------------------------------------------------------ **
  124.    * This function uses a non-recursive algorithm to add a new element to the
  125.    * splay tree.
  126.    *
  127.    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
  128.    *                       the root of the tree to which NewNode is to be added.
  129.    *           NewNode  -  a pointer to an ubi_btNode structure that is NOT
  130.    *                       part of any tree.
  131.    *           ItemPtr  -  A pointer to the sort key that is stored within
  132.    *                       *NewNode.  ItemPtr MUST point to information stored
  133.    *                       in *NewNode or an EXACT DUPLICATE.  The key data
  134.    *                       indicated by ItemPtr is used to place the new node
  135.    *                       into the tree.
  136.    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
  137.    *                       the tree, a duplicate node may be found.  If
  138.    *                       duplicates are allowed, then the new node will
  139.    *                       be simply placed into the tree.  If duplicates
  140.    *                       are not allowed, however, then one of two things
  141.    *                       may happen.
  142.    *                       1) if overwritting *is not* allowed, this
  143.    *                          function will return FALSE (indicating that
  144.    *                          the new node could not be inserted), and
  145.    *                          *OldNode will point to the duplicate that is
  146.    *                          still in the tree.
  147.    *                       2) if overwritting *is* allowed, then this
  148.    *                          function will swap **OldNode for *NewNode.
  149.    *                          In this case, *OldNode will point to the node
  150.    *                          that was removed (thus allowing you to free
  151.    *                          the node).
  152.    *                          **  If you are using overwrite mode, ALWAYS  **
  153.    *                          ** check the return value of this parameter! **
  154.    *                 Note: You may pass NULL in this parameter, the
  155.    *                       function knows how to cope.  If you do this,
  156.    *                       however, there will be no way to return a
  157.    *                       pointer to an old (ie. replaced) node (which is
  158.    *                       a problem if you are using overwrite mode).
  159.    *
  160.    *  Output:  a boolean value indicating success or failure.  The function
  161.    *           will return FALSE if the node could not be added to the tree.
  162.    *           Such failure will only occur if duplicates are not allowed,
  163.    *           nodes cannot be overwritten, AND a duplicate key was found
  164.    *           within the tree.
  165.    * ------------------------------------------------------------------------ **
  166.    */
  167.  
  168. ubi_btNodePtr ubi_sptRemove( ubi_btRootPtr RootPtr, ubi_btNodePtr DeadNode );
  169.   /* ------------------------------------------------------------------------ **
  170.    * This function removes the indicated node from the tree.
  171.    *
  172.    *  Input:   RootPtr  -  A pointer to the header of the tree that contains
  173.    *                       the node to be removed.
  174.    *           DeadNode -  A pointer to the node that will be removed.
  175.    *
  176.    *  Output:  This function returns a pointer to the node that was removed
  177.    *           from the tree (ie. the same as DeadNode).
  178.    *
  179.    *  Note:    The node MUST be in the tree indicated by RootPtr.  If not,
  180.    *           strange and evil things will happen to your trees.
  181.    * ------------------------------------------------------------------------ **
  182.    */
  183.  
  184. ubi_btNodePtr ubi_sptLocate( ubi_btRootPtr RootPtr,
  185.                              ubi_btItemPtr FindMe,
  186.                              ubi_trCompOps CompOp );
  187.   /* ------------------------------------------------------------------------ **
  188.    * The purpose of ubi_btLocate() is to find a node or set of nodes given
  189.    * a target value and a "comparison operator".  The Locate() function is
  190.    * more flexible and (in the case of trees that may contain dupicate keys)
  191.    * more precise than the ubi_btFind() function.  The latter is faster,
  192.    * but it only searches for exact matches and, if the tree contains
  193.    * duplicates, Find() may return a pointer to any one of the duplicate-
  194.    * keyed records.
  195.    *
  196.    *  Input:
  197.    *     RootPtr  -  A pointer to the header of the tree to be searched.
  198.    *     FindMe   -  An ubi_btItemPtr that indicates the key for which to
  199.    *                 search.
  200.    *     CompOp   -  One of the following:
  201.    *                    CompOp     Return a pointer to the node with
  202.    *                    ------     ---------------------------------
  203.    *                   ubi_trLT - the last key value that is less
  204.    *                              than FindMe.
  205.    *                   ubi_trLE - the first key matching FindMe, or
  206.    *                              the last key that is less than
  207.    *                              FindMe.
  208.    *                   ubi_trEQ - the first key matching FindMe.
  209.    *                   ubi_trGE - the first key matching FindMe, or the
  210.    *                              first key greater than FindMe.
  211.    *                   ubi_trGT - the first key greater than FindMe.
  212.    *  Output:
  213.    *     A pointer to the node matching the criteria listed above under
  214.    *     CompOp, or NULL if no node matched the criteria.
  215.    *
  216.    *  Notes:
  217.    *     In the case of trees with duplicate keys, Locate() will behave as
  218.    *     follows:
  219.    *
  220.    *     Find:  3                       Find: 3
  221.    *     Keys:  1 2 2 2 3 3 3 3 3 4 4   Keys: 1 1 2 2 2 4 4 5 5 5 6
  222.    *                  ^ ^         ^                   ^ ^
  223.    *                 LT EQ        GT                 LE GE
  224.    *
  225.    *     That is, when returning a pointer to a node with a key that is LESS
  226.    *     THAN the target key (FindMe), Locate() will return a pointer to the
  227.    *     LAST matching node.
  228.    *     When returning a pointer to a node with a key that is GREATER
  229.    *     THAN the target key (FindMe), Locate() will return a pointer to the
  230.    *     FIRST matching node.
  231.    *
  232.    *  See Also: ubi_btFind(), ubi_btFirstOf(), ubi_btLastOf().
  233.    * ------------------------------------------------------------------------ **
  234.    */
  235.  
  236. ubi_btNodePtr ubi_sptFind( ubi_btRootPtr RootPtr,
  237.                            ubi_btItemPtr FindMe );
  238.   /* ------------------------------------------------------------------------ **
  239.    * This function performs a non-recursive search of a tree for any node
  240.    * matching a specific key.
  241.    *
  242.    *  Input:
  243.    *     RootPtr  -  a pointer to the header of the tree to be searched.
  244.    *     FindMe   -  a pointer to the key value for which to search.
  245.    *
  246.    *  Output:
  247.    *     A pointer to a node with a key that matches the key indicated by
  248.    *     FindMe, or NULL if no such node was found.
  249.    *
  250.    *  Note:   In a tree that allows duplicates, the pointer returned *might
  251.    *          not* point to the (sequentially) first occurance of the
  252.    *          desired key.  In such a tree, it may be more useful to use
  253.    *          ubi_sptLocate().
  254.    * ------------------------------------------------------------------------ **
  255.    */
  256.  
  257. void ubi_sptSplay( ubi_btRootPtr RootPtr,
  258.                    ubi_btNodePtr SplayMe );
  259.   /* ------------------------------------------------------------------------ **
  260.    *  This function allows you to splay the tree at a given node, thus moving
  261.    *  the node to the top of the tree.
  262.    *
  263.    *  Input:
  264.    *     RootPtr  -  a pointer to the header of the tree to be splayed.
  265.    *     SplayMe  -  a pointer to a node within the tree.  This will become
  266.    *                 the new root node.
  267.    *  Output: None.
  268.    *
  269.    *  Notes:  This is an uncharacteristic function for this group of modules
  270.    *          in that it provides access to the internal balancing routines,
  271.    *          which would normally be hidden.
  272.    *          Splaying the tree will not damage it (assuming that I've done
  273.    *          *my* job), but there is overhead involved.  I don't recommend
  274.    *          that you use this function unless you understand the underlying
  275.    *          Splay Tree principles involved.
  276.    * ------------------------------------------------------------------------ **
  277.    */
  278.  
  279. int ubi_sptModuleID( int size, char *list[] );
  280.   /* ------------------------------------------------------------------------ **
  281.    * Returns a set of strings that identify the module.
  282.    *
  283.    *  Input:  size  - The number of elements in the array <list>.
  284.    *          list  - An array of pointers of type (char *).  This array
  285.    *                  should, initially, be empty.  This function will fill
  286.    *                  in the array with pointers to strings.
  287.    *  Output: The number of elements of <list> that were used.  If this value
  288.    *          is less than <size>, the values of the remaining elements are
  289.    *          not guaranteed.
  290.    *
  291.    *  Notes:  Please keep in mind that the pointers returned indicate strings
  292.    *          stored in static memory.  Don't free() them, don't write over
  293.    *          them, etc.  Just read them.
  294.    * ------------------------------------------------------------------------ **
  295.    */
  296.  
  297. /* -------------------------------------------------------------------------- **
  298.  * Masquarade...
  299.  *
  300.  * This set of defines allows you to write programs that will use any of the
  301.  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
  302.  * Instead of using ubi_bt..., use ubi_tr..., and select the tree type by
  303.  * including the appropriate module header.
  304.  */
  305.  
  306. #undef ubi_trInsert
  307. #undef ubi_trRemove
  308. #undef ubi_trLocate
  309. #undef ubi_trFind
  310. #undef ubi_trModuleID
  311.  
  312. #define ubi_trInsert( Rp, Nn, Ip, On ) \
  313.         ubi_sptInsert( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Nn), \
  314.                        (ubi_btItemPtr)(Ip), (ubi_btNodePtr *)(On) )
  315.  
  316. #define ubi_trRemove( Rp, Dn ) \
  317.         ubi_sptRemove( (ubi_btRootPtr)(Rp), (ubi_btNodePtr)(Dn) )
  318.  
  319. #define ubi_trLocate( Rp, Ip, Op ) \
  320.         ubi_sptLocate( (ubi_btRootPtr)(Rp), \
  321.                        (ubi_btItemPtr)(Ip), \
  322.                        (ubi_trCompOps)(Op) )
  323.  
  324. #define ubi_trFind( Rp, Ip ) \
  325.         ubi_sptFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
  326.  
  327. #define ubi_trModuleID( s, l ) ubi_sptModuleID( s, l )
  328.  
  329. /* ================================ The End ================================= */
  330. #endif /* ubi_SplayTree_H */
  331.  
  332.  
  333.  
  334.  
  335.  
  336.